<html>
<head><meta charset="utf-8"><title>borrowing individual fields mutably from raw pointer rece... · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrowing.20individual.20fields.20mutably.20from.20raw.20pointer.20rece.2E.2E.2E.html">borrowing individual fields mutably from raw pointer rece...</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="179884956"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrowing%20individual%20fields%20mutably%20from%20raw%20pointer%20rece.../near/179884956" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matt1992 <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrowing.20individual.20fields.20mutably.20from.20raw.20pointer.20rece.2E.2E.2E.html#179884956">(Nov 04 2019 at 22:40)</a>:</h4>
<p>I am wondering whether this toy example is intended to work(it works fine in MIRI),I am currently relying on this not being UB:</p>
<div class="codehilite"><pre><span></span><span class="k">struct</span> <span class="nc">Foo</span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">a</span>:<span class="nb">String</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">b</span>:<span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">u32</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">unsafe</span><span class="w"> </span><span class="k">fn</span> <span class="nf">borrow_field_a</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="o">&gt;</span><span class="p">(</span><span class="n">this</span>:<span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">Foo</span><span class="p">)</span>-&gt;<span class="kp">&amp;</span><span class="na">&#39;a</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="nb">String</span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="p">(</span><span class="o">*</span><span class="n">this</span><span class="p">).</span><span class="n">a</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">unsafe</span><span class="w"> </span><span class="k">fn</span> <span class="nf">borrow_field_b</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="o">&gt;</span><span class="p">(</span><span class="n">this</span>:<span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">Foo</span><span class="p">)</span>-&gt;<span class="kp">&amp;</span><span class="na">&#39;a</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">u32</span><span class="o">&gt;</span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="p">(</span><span class="o">*</span><span class="n">this</span><span class="p">).</span><span class="n">b</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">(){</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">foo</span><span class="o">=</span><span class="n">Foo</span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">a</span>:<span class="s">&quot;hello&quot;</span><span class="p">.</span><span class="n">into</span><span class="p">(),</span><span class="w"></span>
<span class="w">        </span><span class="n">b</span>:<span class="nc">vec</span><span class="o">!</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">],</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">ptr</span><span class="o">=&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">Foo</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="o">=</span><span class="k">unsafe</span><span class="p">{</span><span class="w"> </span><span class="n">borrow_field_a</span><span class="p">(</span><span class="n">ptr</span><span class="p">)</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="o">=</span><span class="k">unsafe</span><span class="p">{</span><span class="w"> </span><span class="n">borrow_field_b</span><span class="p">(</span><span class="n">ptr</span><span class="p">)</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="n">dbg</span><span class="o">!</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="179913543"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrowing%20individual%20fields%20mutably%20from%20raw%20pointer%20rece.../near/179913543" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrowing.20individual.20fields.20mutably.20from.20raw.20pointer.20rece.2E.2E.2E.html#179913543">(Nov 05 2019 at 09:15)</a>:</h4>
<p>yes, this is intended to work. your mutable refs are both derived from <code>ptr</code>, and the two mutable refs don't overlap.</p>



<a name="179913547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrowing%20individual%20fields%20mutably%20from%20raw%20pointer%20rece.../near/179913547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrowing.20individual.20fields.20mutably.20from.20raw.20pointer.20rece.2E.2E.2E.html#179913547">(Nov 05 2019 at 09:15)</a>:</h4>
<p>I think I'll add this to Miri's test suite ;)</p>



<a name="179913564"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrowing%20individual%20fields%20mutably%20from%20raw%20pointer%20rece.../near/179913564" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrowing.20individual.20fields.20mutably.20from.20raw.20pointer.20rece.2E.2E.2E.html#179913564">(Nov 05 2019 at 09:15)</a>:</h4>
<p>that said, Miri is not normative. so still remain as careful as you can.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>